<template>
	<view>
		<view class="wrap">
			<u-search placeholder="请输入关键字" @search="searchKey" @custom="searchKey"></u-search>
			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom"
				v-if="salseList.length !== 0">
				<view class="page-box">
					<view class="order" v-for="(item, index) in salseList" :key="item.id">
						<view class="item">

							<view class="content">
								<view class="title u-line-2">{{ item.itemAlias? item.itemAlias:item.itemName }}
								</view>
								<view class="type">sku: {{ item.skuCode }}</view>
								<view class="type">品牌: {{ item.itemBrand }}</view>
								<view class="type">尺码: {{ item.itemSize }}</view>
								<view class="type">价格: {{ item.itemPrice }}</view>
								<view class="type">售价: {{ item.salesPrice }}</view>
								<view class="type">利润: {{ item.profit }}</view>
								<view class="type">描述: {{ item.itemDesc }}</view>
								<view class="delivery-time">下单时间: {{ item.placedAt }}</view>
							</view>
							<view class="right">
								<image :src="item.itemImg" mode="aspectFill"></image>
							</view>
						</view>
						<view class="bottom">
							<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
							</view>
							<view class="evaluate btn" @click="showSalesPriceModal(item)">销售出库</view>
							<view class="evaluate btn" @click="copyItme(item)">复制</view>
							<view class="logistics btn" @click="editSalesPrice(item.id)">销售出库</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
				</view>
			</scroll-view>
			<scroll-view scroll-y style="height: 100%;width: 100%;" v-else>
				<view class="page-box">
					<view>
						<view class="centre">
							<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
							</image>
							<view class="explain">
								您还没有相关的订单
								<view class="tips">可以去看看有那些想买的</view>
							</view>
							<view class="btn">随便逛逛</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>

	<u-modal :show="salesPriceShow" title="销售出库" content="内容" @confirm="submit" @cancel="salesPriceShow=false"
		:showCancelButton="true" ref="uModal" :asyncClose="false">
		<up-form ref="form1" :model="salesPriceItem" label-width="60" class="form-container">
			<up-form-item label="编码" prop="skuCode" required >
				<up-input v-model="salesPriceItem.skuCode" placeholder="请输入编码" type="textarea" border :height="10" 
					readonly />
			</up-form-item>
			<up-form-item label="名称" prop="itemName" required margin="10">
				<up-input v-model="salesPriceItem.itemName" placeholder="请输入名称" type="textarea" border :height="10" 
					readonly />
			</up-form-item>
			<up-form-item label="尺码" prop="itemSize" required>
				<up-input v-model="salesPriceItem.itemSize" placeholder="请输入尺码" type="texta" border :height="10"
					readonly />
			</up-form-item>
			<up-form-item label="品牌" prop="itemBrand" required>
				<up-input v-model="salesPriceItem.itemBrand" placeholder="请输入品牌" type="textarea" border :height="10"
					readonly />
			</up-form-item>
			<up-form-item label="成本价" prop="itemPrice" required>
				<up-input v-model="salesPriceItem.itemPrice" placeholder="请输入单价" type="textarea" border :height="10"
					readonly />
			</up-form-item>
			<up-form-item label="售价" prop="salesPrice">
				<up-input v-model="salesPriceItem.salesPrice" placeholder="请输入售价" type="textarea" border="surround"
					:height="10" />
			</up-form-item>
			<up-form-item label="销售数量" prop="num">
				<up-input v-model="salesPriceItem.num" type="textarea" border :height="10" />
			</up-form-item>
			<up-form-item label="利润" prop="profit">
				<up-input v-model="subtraction" border :height="10" readonly />
			</up-form-item>
			<up-form-item label="单号" prop="sentExpressNo">
				<up-input v-model="salesPriceItem.sentExpressNo" border="surround"></up-input>
				<template #right>
					<up-icon name="scan" size="25" @click="scanCode"></up-icon>
				</template>
			</up-form-item>
		</up-form>
	</u-modal>
</template>



<script>
	import {
		listSales,
		addRecords,
		salesOrder
	} from "@/api/work/sales/sales.js";

	export default {
		data() {
			return {
				nextPageFlag: true,
				query: {
					pageNum: 1,
					pageSize: 10,
					orderStatus: ''
				},
				salesPriceShow: false,
				salesPriceItem: {
					salesPrice: '',
					sentExpressNo: ''
				},
				form: {
					id: '',
					salesPrice: '',
					itemPrice: '',
					num:'',
					sentExpressNo: '',
				},
				salseList: [],
				totalCount: 0,
				current: 0,
				swiperCurrent: 0,
				rules: {
					salesPrice: {
						required: true,
						type: 'number',
						message: '请填写售价',
						trigger: ['blur', 'change']
					},
				},
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
			};
		},
		onLoad(event) {
			this.searchKey(event.skuName);
		},
		computed: {
			subtraction() {
				return !this.salesPriceItem.salesPrice ? 0 : ((this.salesPriceItem.salesPrice - this.salesPriceItem
					.itemPrice) * this.salesPriceItem.num).toFixed(2);
			}
		},
		methods: {
			submit() {
				// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
				this.$refs.form1.setRules(this.rules)
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {
					this.form.id = this.salesPriceItem.id
					this.form.salesPrice = this.salesPriceItem.salesPrice
					this.form.itemPrice = this.salesPriceItem.itemPrice
					this.form.sentExpressNo = this.salesPriceItem.sentExpressNo
					this.form.num = this.salesPriceItem.num
					salesOrder(this.form).then(response => {
						if (response.code == 200) {
							uni.$u.toast('修改成功')
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/sales/index'
								})
							}, 500);
						}
					})
				}).catch(errors => {
					console.log(errors)
					uni.$u.toast('校验失败')
				})
			},
			scanCode() {
				const _self = this
				// 允许从相机和相册扫码
				uni.scanCode({
					success: function(res) {
						if (res.result) {
							_self.salesPriceItem.sentExpressNo = res.result
						}
					}
				});
			},
			// 触底懒加载
			reachBottom() {
				console.info("触底----开始加在salseList")
				this.loadStatus.splice(this.current, 1, "loading")
				if (this.nextPageFlag) {
					setTimeout(() => {
						console.info("延迟1.1s")
						this.getSalesList(this.current);
					}, 1100);
				}

			},
			searchKey(keyword) {
				this.getSalesList(keyword)
			},
			editSalesPrice(id) {
				uni.navigateTo({
					url: `/pages/sales/editPrice?id=${id}`
				});
			},
			showSalesPriceModal(item) {
				//改为模态框修改,重置售价数量
				this.salesPriceItem = item;
				this.salesPriceItem.num = 1;
				this.salesPriceShow = true;
			},
			copyItme(row) {
				// this.$modal.confirm('确认复制条目'+item.id+'吗?').then(() => {
				//   const req = item
				// })
				this.$modal.confirm('是否确复制销售记录编号为"' + row.id + '"的数据项？').then(function() {
					const time = new Date()
					row.id = null;
					row.orderDetailId = null;
					row.thirdOrderNo = null;
					row.profit = null;
					row.salesPrice = null;
					row.updateTime = null;
					row.placedYear = time.getFullYear();
					row.receiveTime = time;
					row.placedAt = time;
					return addRecords(row);
				}).then(() => {
					this.getSalesList();
					this.$modal.msgSuccess("复制成功");
				}).catch(() => {});
			},
			// 页面数据
			getSalesList(keyword) {
				if (keyword) {
					this.query.keyword = keyword
					this.query.pageNum = 1
					this.salseList = []
				}
				this.nextPageFlag = false;
				const pageTotal = this.totalCount / this.query.pageSize
				if (true) {
					setTimeout(() => {
						listSales(this.query).then(response => {
							this.salseList.push(...response.rows)
							this.totalCount = response.total
							//如果 rows.length<pageSize,说明没有下一页了
							if (response.rows.length < this.query.pageSize) {
								this.nextPageFlag = false;
							} else {
								this.query.pageNum++
								this.nextPageFlag = true;
							}
						})
					}, 500)
					this.loadStatus.splice(this.current, 1, "loadmore")
				} else {
					this.loadStatus.splice(this.current, 1, "noMore")
				}
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.right {
				margin-top: 100rpx;
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				flex: 1;

				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
	.form-container up-form-item + up-form-item {
	 height: 10px; /* 设置上边距为15px */
	}
</style>